package com.googlecode.wicketcharts.showcase;

import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.googlecode.wicketcharts.JavaScriptResourceRegistry;
import com.googlecode.wicketcharts.highcharts.Chart;
import com.googlecode.wicketcharts.highcharts.options.Options;
import com.googlecode.wicketcharts.highcharts.theme.Theme;
import com.googlecode.wicketcharts.showcase.links.UpdateChartLink;
import com.googlecode.wicketcharts.showcase.options.AreaWithNegativeValuesOptions;
import com.googlecode.wicketcharts.showcase.options.BasicAreaOptions;
import com.googlecode.wicketcharts.showcase.options.BasicLineOptions;
import com.googlecode.wicketcharts.showcase.options.ComboOptions;
import com.googlecode.wicketcharts.showcase.options.DonutOptions;
import com.googlecode.wicketcharts.showcase.options.LineWithDataLabelsOptions;
import com.googlecode.wicketcharts.showcase.options.LogarithmicAxisOptions;
import com.googlecode.wicketcharts.showcase.options.PolarOptions;
import com.googlecode.wicketcharts.showcase.options.ScatterPlotOptions;
import com.googlecode.wicketcharts.showcase.options.SplineWithInvertedAxisOptions;
import com.googlecode.wicketcharts.showcase.options.SplineWithPlotBandsOptions;
import com.googlecode.wicketcharts.showcase.options.SplineWithSymbolsOptions;
import com.googlecode.wicketcharts.showcase.options.StackedAndGroupedColumnOptions;
import com.googlecode.wicketcharts.showcase.options.TimeDataWithIrregularIntervalsOptions;
import com.googlecode.wicketcharts.showcase.options.ZoomableTimeSeriesOptions;
import com.googlecode.wicketcharts.showcase.theme.GrayTheme;
import com.googlecode.wicketcharts.showcase.theme.GridTheme;
import com.googlecode.wicketcharts.showcase.theme.SkiesTheme;

public class Homepage extends WebPage {

  private static final long serialVersionUID = 1L;

  public Homepage(final PageParameters parameters) {
    Theme theme = this
        .getThemeFromParams(parameters);
    Options options = this
        .getOptionsToDisplay();
    final Chart chart = new Chart("chart", options, theme);
    this
        .add(chart);
    Label codeContainer = this
        .addCodeContainer();
    this
        .addChartLinks(chart, codeContainer);
  }

  private void addChartLinks(final Chart chart, final Label codeContainer) {
    this
        .add(new UpdateChartLink("line", chart, codeContainer,
            new BasicLineOptions()));
    this
        .add(new UpdateChartLink("splineWithSymbols", chart, codeContainer,
            new SplineWithSymbolsOptions()));
    this
        .add(new UpdateChartLink("irregularIntervals", chart, codeContainer,
            new TimeDataWithIrregularIntervalsOptions()));
    this
        .add(new UpdateChartLink("logarithmicAxis", chart, codeContainer,
            new LogarithmicAxisOptions()));
    this
        .add(new UpdateChartLink("scatter", chart, codeContainer,
            new ScatterPlotOptions()));

    this
        .add(new UpdateChartLink("area", chart, codeContainer,
            new BasicAreaOptions()));
    this
        .add(new UpdateChartLink("areaWithNegativeValues", chart,
            codeContainer, new AreaWithNegativeValuesOptions()));

    this
        .add(new UpdateChartLink("stackedColumn", chart, codeContainer,
            new StackedAndGroupedColumnOptions()));
    this
        .add(new UpdateChartLink("combo", chart, codeContainer,
            new ComboOptions()));
    this
        .add(new UpdateChartLink("donut", chart, codeContainer,
            new DonutOptions()));
    this
        .add(new UpdateChartLink("withDataLabels", chart, codeContainer,
            new LineWithDataLabelsOptions()));
    this
        .add(new UpdateChartLink("zoomableTimeSeries", chart, codeContainer,
            new ZoomableTimeSeriesOptions()));
    this
        .add(new UpdateChartLink("splineInverted", chart, codeContainer,
            new SplineWithInvertedAxisOptions()));
    this
        .add(new UpdateChartLink("splineWithPlotBands", chart, codeContainer,
            new SplineWithPlotBandsOptions()));
    this
        .add(new UpdateChartLink("polar", chart, codeContainer,
            new PolarOptions()));
  }

  private Label addCodeContainer() {
    Label codeContainer = new Label("code", new StringFromResourceModel(
        BasicLineOptions.class, BasicLineOptions.class.getSimpleName()
            + ".java"));
    codeContainer
        .setOutputMarkupId(true);
    this
        .add(codeContainer);
    return codeContainer;
  }

  private Options getOptionsToDisplay() {
    Options options = ((ShowcaseSession) this
        .getSession())
        .getCurrentChartOptions();
    if (options == null) {
      options = new BasicLineOptions();
    }
    return options;
  }

  private int getSelectedTab() {
    String theme = this
        .getPageParameters()
        .get("theme")
        .toString();
    if ("grid"
        .equals(theme)) {
      return 1;
    } else if ("skies"
        .equals(theme)) {
      return 2;
    } else if ("gray"
        .equals(theme)) {
      return 3;
    } else if ("darkblue"
        .equals(theme)) {
      return 4;
    } else if ("darkgreen"
        .equals(theme)) {
      return 5;
    } else {
      return 0;
    }
  }

  private Theme getThemeFromParams(final PageParameters params) {
    String themeString = params
        .get("theme")
        .toString();
    if ("grid"
        .equals(themeString)) {
      return new GridTheme();
    } else if ("skies"
        .equals(themeString)) {
      return new SkiesTheme();
    } else if ("gray"
        .equals(themeString)) {
      return new GrayTheme();
    } else {
      // default theme
      return null;
    }
  }

  @Override
  public void renderHead(final IHeaderResponse response) {
    response
        .renderCSSReference("css/style.css");
    response
        .renderCSSReference("css/cupertino/jquery-ui-1.8.23.custom.css");
    JavaScriptResourceRegistry
        .getInstance()
        .getJQueryEntry()
        .addToHeaderResponse(response);
    response
        .renderJavaScriptReference("js/jquery-ui-1.8.23.custom.min.js");
    response
        .renderJavaScriptReference("js/wicket-charts.js");

    // add jquery tabs with current theme selected
    int selectedTab = this
        .getSelectedTab();
    response
        .renderOnDomReadyJavaScript("$('#tabs').tabs({select: function(event, ui){location.href = ui.tab.rel;}, selected: "
            + selectedTab + "})");
  }
}
